/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.utils.validation;

import org.jdesktop.beansbinding.*;
import org.jdesktop.jxlayer.JXLayer;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE;

/**
 * Created by Peter on 2016/8/31.
 */
public class GuiComponentFactory {

    /**
     * Create TextField JxLayer.
     *
     * @param bindingGroup   group
     * @param sourceObject   source
     * @param sourceProperty property
     * @return JxLayer
     */
    public static JXLayer<JTextComponent> createTextField(BindingGroup bindingGroup, Object sourceObject, String sourceProperty) {
        JTextField field = new JTextField();
        AutoBinding binding = Bindings.createAutoBinding(READ_WRITE, sourceObject, ELProperty.create("${" + sourceProperty + "}"), field, BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        bindingGroup.bind();

        return new JXLayer<JTextComponent>(field, new ValidationUI(sourceObject, sourceProperty));
    }

    /**
     * Create Combobox JxLayer.
     *
     * @param bindingGroup   group
     * @param sourceObject   source
     * @param sourceProperty property
     * @return JxLayer
     */
    public static JXLayer<JComboBox> createCombobox(BindingGroup bindingGroup, Object sourceObject, String sourceProperty) {
        JComboBox field = new JComboBox();
        AutoBinding binding = Bindings.createAutoBinding(READ_WRITE, sourceObject, ELProperty.create("${" + sourceProperty + "}"), field, BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        bindingGroup.bind();

        return new JXLayer<JComboBox>(field, new ValidationUI(sourceObject, sourceProperty));
    }

    /**
     * Create CheckBox JxLayer.
     *
     * @param bindingGroup   group
     * @param sourceObject   source
     * @param sourceProperty property
     * @return JxLayer
     */
    public static JXLayer<JCheckBox> createCheckBox(BindingGroup bindingGroup, Object sourceObject, String sourceProperty) {
        JCheckBox field = new JCheckBox();
        AutoBinding binding = Bindings.createAutoBinding(READ_WRITE, sourceObject, ELProperty.create("${" + sourceProperty + "}"), field, BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);
        bindingGroup.bind();

        return new JXLayer<JCheckBox>(field, new ValidationUI(sourceObject, sourceProperty));
    }

    /**
     * Create PasswordTextField JxLayer.
     *
     * @param bindingGroup   group
     * @param sourceObject   source
     * @param sourceProperty property
     * @return JxLayer
     */
    public static JXLayer<JTextComponent> createPasswordTextField(BindingGroup bindingGroup, Object sourceObject, String sourceProperty) {
        JPasswordField field = new JPasswordField();
        AutoBinding binding = Bindings.createAutoBinding(READ_WRITE, sourceObject, ELProperty.create("${" + sourceProperty + "}"), field, BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        bindingGroup.bind();
        return new JXLayer<JTextComponent>(field, new ValidationUI(sourceObject, sourceProperty));
    }

}
